package com.aaa.train.controller;


import com.aaa.common.entity.LayUiTable;
import com.aaa.common.entity.ResultData;
import com.aaa.common.entity.TbAssessmentResult;
import com.aaa.common.entity.TbTrainer;
import com.aaa.train.service.TbAssessmentResultService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考核结果表 前端控制器
 * </p>
 *
 * @author wang
 * @since 2020-07-29
 */
@Controller
@RequestMapping("/ar")
public class TbAssessmentResultController {

    @Autowired
    private TbAssessmentResultService resultService;

    @ResponseBody
    @RequestMapping("/selectAll")
    public LayUiTable selectAll(Integer page, Integer limit,String userName){
        LayUiTable layUiTable= resultService.selectAll(page,limit,userName);
        return layUiTable;
    }

    @ResponseBody
    @RequestMapping("/selectTrainer")
    public List<Map> selectTrainer(){
         return resultService.selectTrainer();

    }
    @ResponseBody
    @RequestMapping("/selectUser")
    public List<Map> selectUser(){
        return resultService.selectUser();
    }



    @ResponseBody
    @RequestMapping("/insertResult")
    public ResultData insert(TbAssessmentResult result){
        boolean insert = resultService.insert(result);
        if (insert){
            return  new ResultData(true,"新增",null);
        }else{
            return  new ResultData(false,"新增失败",null);
        }
    }


    @ResponseBody
    @RequestMapping("/update")
    public ResultData update(TbAssessmentResult result){
        Wrapper<TbAssessmentResult> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",result.getUserId());
        boolean update;
        if (result.getTbArExamination() >= 60){
            System.out.println("大于60");
            result.setStatus(0);
            update = resultService.update(result, wrapper);
        }else {
            System.out.println("小于60");
            result.setStatus(1);
            update = resultService.update(result,wrapper);
        }
        if (update){
            return new ResultData(true,"修改成功",null);
        }else {
            return  new ResultData(false,"修改失败",null);
        }
    }

    @ResponseBody
    @RequestMapping("/selectByUserId")
    public ResultData selectByUserId(TbAssessmentResult result){
        Wrapper<TbAssessmentResult> wrapper = new EntityWrapper<>();
        System.out.println(result.getUserId());
        wrapper.eq("user_id",result.getUserId());
        List insert = resultService.selectList(wrapper);
        if (insert.size()>0){
            return  new ResultData(false,"已培训",null);
        }else{
            return  new ResultData(true,"未培训",null);
        }
    }
}

